<template>
  <div class="flex direction-column height-100">
    <div class="search-form">
      <el-form inline>
        <el-form-item label="报表类型">
          <el-select v-model="type">
            <el-option :value="1" label="月报"></el-option>
            <el-option :value="2" label="日报"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="统计区间">
          <el-date-picker
            :type="pickerType" v-model="value"
            :format="pickerFormatter" :value-format="pickerFormatter"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <h1 class="table-title text-center">{{ tableTitle }}</h1>
    <div class="flex justify-between table-desc padding-vertical">
      <span>统计范围：{{ range }}</span>
      <span>统计区间：{{ value }}</span>
    </div>
    <el-table
      class="table" v-loading="loading" :data="lists" height="100%"
      :header-cell-style="{
        backgroundColor: themeColor,
        color: 'white'
      }"
    >
      <div slot="empty" class="empty-lists">
        <svg-icon name="search"></svg-icon>
        <span class="empty-text">暂无数据</span>
      </div>
      <el-table-column v-for="column in columns" :key="column.label" v-bind="column"></el-table-column>
    </el-table>
  </div>
</template>

<style lang="scss" scoped>
.table-title {
  font-weight: 500;
}
.table-desc {
  color: gray;
}
</style>

<script>
export default {
  name: 'statistical-table-of-energy-consumption',
  data () {
    return {
      type: 1,
      value: this.$dayjs().format('YYYY-MM'),
      range: '全厂',
      loading: false,
      lists: [],
      columns: [
        { label: '用能单位', prop: 'col1' },
        { label: '其它原煤（吨）', prop: 'col2' },
        { label: '电力（千瓦时）', prop: 'col3' },
        { label: '综合能耗（吨标准煤）', prop: 'col4' },
      ],
    }
  },
  computed: {
    themeColor () {
      return this.$store.state.user.theme
    },
    pickerType () {
      const { type } = this
      return type === 1 ? 'month' : 'date'
    },
    pickerFormatter () {
      const { type } = this
      return type === 1 ? 'yyyy-MM' : 'yyyy-MM-dd'
    },
    tableTitle () {
      const { type } = this
      return `能耗量统计${type === 1 ? '月报' : '日报'}表`
    },
  },
  watch: {
    type (val) {
      const value = this.value || undefined
      this.value = this.$dayjs(value).format(val === 1 ? 'YYYY-MM' : 'YYYY-MM-DD')
    }
  },
  mounted () {
    this.getLists()
  },
  methods: {
    getLists () {
      this.loading = true
      // TODO
      return new Promise((resolve) => {
        setTimeout(() => {
          this.loading = false
          resolve()
          this.lists = [
            {
              col1: '全厂',
              col2: '192.8',
              col3: '14760',
              col4: '0'
            },
            {
              col1: '高压配电房',
              col2: '192.8',
              col3: '14760',
              col4: '0'
            },
            {
              col1: '皮带秤',
              col2: '192.8',
              col3: '14760',
              col4: '0'
            },
          ]
        }, 400)
      })
    },
  },
}
</script>
